<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 4/10/17
 * Time: 4:23 PM
 */

namespace common\models;
use common\components\bank\BankApi;
use common\components\bank\Icbc;
use common\components\bank\icbc\IcbcZjfh;
use yii\base\ErrorException;

/**
 * Class BankGuaranteeCompanyBind
 * @package common\models
 *
 * @property BankApi $cooperate 合作机构，业务主要在此，暂时规定组件实例驱动与数据库一一对应
 * @property Bank $bank
 * @property GuaranteeCompany $guaranteeCompany
 */
class BankGuaranteeCompanyBind extends \common\models\base\BankGuaranteeCompanyBind
{
	/**
	 * @return \yii\db\ActiveQuery
	 */
	public function getBank()
	{
		return $this->hasOne(Bank::className(), ['id' => 'bank_id']);
	}
	
	/**
	 * @return \yii\db\ActiveQuery
	 */
	public function getGuaranteeCompany()
	{
		return $this->hasOne(GuaranteeCompany::className(), ['id' => 'guarantee_company_id']);
	}
	
	private $_cooperate; // 具体合作银行
	
	/**
	 * @return BankApi
	 */
	public function getCooperate()
	{
		return $this->_cooperate;
	}
	
	public function setCooperate(BankApi $cooperate)
	{
		$this->_cooperate = $cooperate;
	}
	
	public function afterFind()
	{
		parent::afterFind(); // TODO: Change the autogenerated stub
		switch ($this->bank->headquarters){
            case Icbc::HEADQUARTERS: $cooperate =  new Icbc();
				$cooperate->instance = $this;
				break;
			default: throw new ErrorException('没有此合作银行', 1040);
				break;
		}
		$this->setCooperate($cooperate);
	}
}